home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dos6mm.zip / H2D.ASM < prev    next >
Assembly Source File  |  1993-03-31  |  8KB  |  215 lines

  1. ;****************************************************************************
  2. ; H2D displays a hexadecimal number in decimal format. Its syntax is:
  3. ;
  4. ;       H2D hex-value
  5. ;
  6. ; where "hex-value" is a hexadecimal value from 0 to FFFFFFFFh.
  7. ;****************************************************************************
  8.  
  9. code            segment
  10.                 assume  cs:code,ds:code
  11.                 org     100h
  12. begin:          jmp     main
  13.  
  14. helpmsg         db      "Displays a hexadecimal number in decimal format."
  15.                 db      13,10,13,10
  16.                 db      "H2D hex-value",13,10,"$"
  17.  
  18. errmsg1         db      "Syntax: H2D hex-value",13,10,"$"
  19. errmsg2         db      "Number too large (cannot exceed FFFFFFFF)",13,10,"$"
  20. errmsg3         db      "Invalid digit"
  21. crlf            db      13,10,"$"
  22.  
  23. loword          dw      0                       ;Accumulator (low word)
  24. hiword          dw      0                       ;Accumulator (high word)
  25.  
  26. ;****************************************************************************
  27. ; Procedure MAIN
  28. ;****************************************************************************
  29.  
  30. main            proc    near
  31.                 cld                             ;Clear direction flag
  32.                 mov     si,81h                  ;Point SI to the command line
  33.                 call    scanhelp                ;Scan for a "/?" switch
  34.                 jnc     main1                   ;Branch if not found
  35.  
  36.                 mov     ah,09h                  ;Display help text
  37.                 mov     dx,offset helpmsg
  38.                 int     21h
  39.                 jmp     exit                    ;Exit
  40.  
  41. main1:          call    findchar                ;Find the start of the string
  42.                 jc      error1                  ;Error if there is no string
  43.                 xor     cx,cx                   ;Zero digit count in CX
  44.  
  45. main2:          lodsb                           ;Get the next digit
  46.                 cmp     al,20h                  ;Branch if it's a space,
  47.                 je      main4                   ;tab, or end-of-line
  48.                 cmp     al,09h                  ;character
  49.                 je      main4
  50.                 cmp     al,0Dh
  51.                 je      main4
  52.  
  53.                 cmp     cx,8                    ;Error if this is the
  54.                 je      error2                  ;ninth digit
  55.                 inc     cx
  56.  
  57.                 call    reduce                  ;ASCII -> binary (DX)
  58.                 jc      error3                  ;Error if it's invalid
  59.  
  60.                 push    cx                      ;Mulitply the current value
  61.                 mov     bx,loword               ;by 16 and add the value
  62.                 mov     ax,hiword               ;of the digit just read
  63.                 mov     cx,4
  64. main3:          shl     bx,1
  65.                 rcl     ax,1
  66.                 loop    main3
  67.                 add     bx,dx
  68.                 adc     ax,0
  69.                 mov     loword,bx
  70.                 mov     hiword,ax
  71.                 pop     cx
  72.                 jmp     main2
  73.  
  74. main4:          mov     bx,loword               ;Display the number
  75.                 mov     ax,hiword
  76.                 call    bin2dec
  77.  
  78. exit:           mov     ax,4C00h                ;Return to DOS
  79.                 int     21h
  80. ;
  81. ; Error handling routines.
  82. ;
  83. error1:         mov     dx,offset errmsg1
  84.                 jmp     short error
  85. error2:         mov     dx,offset errmsg2
  86.                 jmp     short error
  87. error3:         mov     dx,offset errmsg3
  88. error:          mov     ah,09h
  89.                 int     21h
  90.                 jmp     exit
  91. main            endp
  92.  
  93. ;****************************************************************************
  94. ; REDUCE converts the hex digit in AL to a binary value from 0 to 15 in
  95. ; DX. On return, carry set means the digit was not a valid hex character.
  96. ;****************************************************************************
  97.  
  98. reduce          proc    near
  99.                 cmp     al,"0"                  ;Convert "0" thru "9"
  100.                 jb      r_error
  101.                 cmp     al,"9"
  102.                 ja      r1
  103.                 mov     dl,al
  104.                 sub     dl,30h
  105. r_exit:         xor     dh,dh
  106.                 clc
  107.                 ret
  108.  
  109. r1:             cmp     al,"A"                  ;Convert "A" thru "F"
  110.                 jb      r_error
  111.                 cmp     al,"F"
  112.                 ja      r2
  113.                 mov     dl,al
  114.                 sub     dl,55
  115.                 jmp     r_exit
  116.  
  117. r2:             cmp     al,"a"                  ;Convert "a" thru "f"
  118.                 jb      r_error
  119.                 cmp     al,"f"
  120.                 ja      r_error
  121.                 mov     dl,al
  122.                 sub     dl,87
  123.                 jmp     r_exit
  124.  
  125. r_error:        stc                             ;Set carry and exit
  126.                 ret
  127. reduce          endp
  128.  
  129. ;****************************************************************************
  130. ; BIN2DEC displays the number in AX:BX in decimal format.
  131. ;****************************************************************************
  132.  
  133. bin2dec         proc    near
  134.                 xor     cx,cx
  135.                 mov     di,10
  136.  
  137. b2d1:           xor     dx,dx
  138.                 div     di
  139.                 mov     si,ax
  140.                 mov     ax,bx
  141.                 div     di
  142.                 mov     bx,ax
  143.                 mov     ax,si
  144.                 inc     cx
  145.                 push    dx
  146.                 or      ax,ax
  147.                 jnz     b2d1
  148.                 or      bx,ax
  149.                 jnz     b2d1
  150.  
  151. b2d2:           mov     ah,02h
  152.                 pop     dx
  153.                 add     dl,30h
  154.                 int     21h
  155.                 loop    b2d2
  156.  
  157.                 mov     ah,09h
  158.                 mov     dx,offset crlf
  159.                 int     21h
  160.                 ret
  161. bin2dec         endp
  162.  
  163. ;****************************************************************************
  164. ; FINDCHAR advances SI to the next non-white-space character. On return,
  165. ; carry set indicates EOL was encountered; carry clear indicates it was not.
  166. ;****************************************************************************
  167.  
  168. findchar        proc    near
  169.                 lodsb                           ;Get the next character
  170.                 cmp     al,09h                  ;Loop if tab
  171.                 je      findchar
  172.                 cmp     al,20h                  ;Loop if space
  173.                 je      findchar
  174.                 cmp     al,2Ch                  ;Loop if comma
  175.                 je      findchar
  176.                 dec     si                      ;Point SI to the character
  177.                 cmp     al,0Dh                  ;Exit with carry set if end
  178.                 je      eol                     ;of line is reached
  179.  
  180.                 clc                             ;Clear carry and exit
  181.                 ret
  182.  
  183. eol:            stc                             ;Set carry and exit
  184.                 ret
  185. findchar        endp
  186.  
  187. ;****************************************************************************
  188. ; SCANHELP scans the command line for a /? switch. If the switch is found,
  189. ; carry returns set and SI contains the switch offset. If the switch is not
  190. ; found, carry returns clear.
  191. ;****************************************************************************
  192.  
  193. scanhelp        proc    near
  194.                 push    si                      ;Save SI
  195. scanloop:       lodsb                           ;Get a character
  196.                 cmp     al,0Dh                  ;Exit if end of line
  197.                 je      scan_exit
  198.                 cmp     al,"?"                  ;Loop if not "?"
  199.                 jne     scanloop
  200.                 cmp     byte ptr [si-2],"/"     ;Loop if not "/"
  201.                 jne     scanloop
  202.  
  203.                 add     sp,2                    ;Clear the stack
  204.                 sub     si,2                    ;Adjust SI
  205.                 stc                             ;Set carry and exit
  206.                 ret
  207.  
  208. scan_exit:      pop     si                      ;Restore SI
  209.                 clc                             ;Clear carry and exit
  210.                 ret
  211. scanhelp        endp
  212.  
  213. code            ends
  214.                 end     begin
  215.